﻿Imports Exp.DATA_Service
Imports Exp.Model
Public Class UnitType

    Inherits System.Web.UI.Page
    Const MENUNAME As String = "UNITTYPE"
    Const SaveSuccess As String = "Save completed."
    Const SaveError As String = "Save error !"
    Const DeleteSuccess As String = "Delete completed."
    Const DeleteError As String = "Delete not complete"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                Dim objUser As REM_User = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(objUser.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If

            End If
            BindData()


        End If
    End Sub
#Region "Event"
    Protected Sub btn_Save_Click(sender As Object, e As EventArgs) Handles btn_Save.Click
        If HDFieldUnitTypeID.Value = "" Then
            Save()
            BindData()
            txtUnitTypeName.Text = ""
            MultiView1.ActiveViewIndex = 0
            JavaScript.AlertMessage(Me.Page, SaveSuccess)
        Else
            Edit()
            BindData()
            txtUnitTypeName.Text = ""
            MultiView1.ActiveViewIndex = 0
        End If
    End Sub
    Private Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        HDFieldUnitTypeID.Value = ""
        BindData()
    End Sub

    Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
        txtUnitTypeName.Text = ""
        HDFieldUnitTypeID.Value = ""
    End Sub
#End Region
#Region "Function"
    Sub Save()
        Dim objUser As REM_User = CType(Session("USER"), REM_User)
        Dim UnitType_Service As New UnitType_service
        Dim objREM_UnitType As New REM_UnitType
        If txtUnitTypeName.Text <> "" Then
            If CheckNameExisting(txtUnitTypeName.Text, 0) Then
                JavaScript.AlertMessage(Me.Page, "Data existing, please try again")
                Return
            End If
            'Dim iUnitType As Integer
            'iUnitType = UnitType_Service.GetLastID()
            'objREM_UnitType.UnitType_ID = iUnitType
            objREM_UnitType.UnitTypeName = txtUnitTypeName.Text
            objREM_UnitType.CreateDate = CType(Now, Date)
            objREM_UnitType.UpdateDate = CType(Now, Date)
            objREM_UnitType.CreateUser = objUser.User_ID
            objREM_UnitType.UpdateUser = objUser.User_ID
            UnitType_Service.Add(objREM_UnitType)
            JavaScript.AlertMessage(Me.Page, "Save data complete")
        Else
            JavaScript.AlertMessage(Me.Page, "Please insert data, try again")
        End If
    End Sub
    Sub Edit()
        Dim objUser As REM_User = CType(Session("USER"), REM_User)
        Dim objREM_UnitType As New REM_UnitType
        If txtUnitTypeName.Text <> "" Then
            If CheckNameExisting(txtUnitTypeName.Text, HDFieldUnitTypeID.Value) Then
                JavaScript.AlertMessage(Me.Page, "Data existing, please try again")
                Return
            End If
            Dim UnitType_service As New UnitType_service
            objREM_UnitType.UnitType_ID = HDFieldUnitTypeID.Value
            objREM_UnitType.UnitTypeName = txtUnitTypeName.Text
            objREM_UnitType.UpdateDate = CType(Now, Date)
            objREM_UnitType.UpdateUser = objUser.User_ID

            UnitType_service.Edit(objREM_UnitType)
            JavaScript.AlertMessage(Me.Page, "Save Data Complete")
            HDFieldUnitTypeID.Value = ""
        Else
            JavaScript.AlertMessage(Me.Page, "Please insert data, try again")
        End If
    End Sub
    Sub BindData()
        Dim UnitType_Service As New UnitType_service
        Dim objREM_UnitType As New List(Of REM_UnitType)
        objREM_UnitType = UnitType_Service.GetAll()
        gv_Data.DataSource = objREM_UnitType
        gv_Data.DataBind()
    End Sub
    Function CheckNameExisting(ByVal sUnitTypeName As String, ByVal iUnitTypeID As Integer) As Boolean
        Dim UnitType_Service As New UnitType_service
        Dim LobjREM_UnitType As New List(Of REM_UnitType)

        LobjREM_UnitType = UnitType_Service.GetByName(sUnitTypeName)
        If LobjREM_UnitType Is Nothing Then
            Return False
        Else
            If LobjREM_UnitType.Count > 0 Then
                If LobjREM_UnitType.Item(0).UnitType_ID = iUnitTypeID Then
                    Return False
                End If
                Return True
            End If
        End If
        Return False
    End Function
#End Region



#Region "gv_Data"
    Private Sub gv_data_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_data.RowCommand
        Dim commandArgs As String = e.CommandArgument.ToString()
        Dim REM_UnitType As New REM_UnitType
        Dim UnitType_Service As New UnitType_service
        Dim iUnitTypeID As Integer
        iUnitTypeID = CType(commandArgs, Integer)
        If e.CommandName.ToLower = "edit" Then
            REM_UnitType = UnitType_Service.GetByID(iUnitTypeID)

            If Not REM_UnitType Is Nothing Then
                If REM_UnitType.UnitTypeName <> "" Then
                    txtUnitTypeName.Text = REM_UnitType.UnitTypeName
                    HDFieldUnitTypeID.Value = iUnitTypeID
                    MultiView1.ActiveViewIndex = 1
                Else
                    txtUnitTypeName.Text = ""
                End If
            End If

        ElseIf e.CommandName.ToLower = "delete" Then
            UnitType_Service.Delete(iUnitTypeID)
        End If
    End Sub
    Private Sub gv_Data_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_data.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then

                ''CType(e.Row.Cells(18).FindControl("chk_IMMEDIATE"), CheckBox)
                e.Row.Cells(4).Text = Format(CType(CType(e.Row.Cells(4).FindControl("lblCreateDate"), Label).Text, Date), "dd/MM/yyyy")
                e.Row.Cells(5).Text = Format(CType(CType(e.Row.Cells(5).FindControl("lblUpdateDate"), Label).Text, Date), "dd/MM/yyyy")
                GetDataUtil.CDateFormat(e.Row.Cells(4).Text)
                GetDataUtil.CDateFormat(e.Row.Cells(5).Text)
                Dim User_Service As New User_Service
                Dim LEXP_Select_REM_User_Result As New List(Of EXP_Select_REM_User_Result)
                Dim iUserID As Integer = CType(e.Row.Cells(6).FindControl("lblUpdateUser"), Label).Text
                LEXP_Select_REM_User_Result = User_Service.GetAll.Where(Function(p) p.User_ID = iUserID).ToList
                e.Row.Cells(6).Text = LEXP_Select_REM_User_Result.Item(0).FullName
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#99CCFF'")

                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;")

            End If

        Catch ex As Exception

            ' Page.ClientScript.AlertMessage(Me, "gv_Data_RowDataBound", "gv_Data_RowDataBound >>" & ex.Message)

        End Try
    End Sub
    Private Sub gv_data_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles gv_data.RowDeleting
        BindData()
    End Sub
    Private Sub gv_data_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles gv_data.RowEditing
        BindData()
    End Sub
#End Region



End Class